package servlet;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MyClassLoader extends ClassLoader {

    private String dirPath;

    private MyClassLoader() {
    }

    public MyClassLoader(String dirPath) {
        this.dirPath = dirPath;
    }

    @Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        String realPath = "file:///" + dirPath + "/" + name.replace(".", "/") + ".class";
        System.out.println(realPath);
        byte[] bytes = null;

        try {
            Path path = Paths.get(new URI(realPath));
            bytes = Files.readAllBytes(path);

        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Class<?> aClass = defineClass(name, bytes, 0, bytes.length);
        return aClass;
    }
}
